package com.photoeditor.demo.ui.widget.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.mklimek.frameviedoview.FrameVideoView;
import com.mklimek.frameviedoview.FrameVideoViewListener;
import com.photoeditor.R;
import com.photoeditor.demo.model.bean.BitmapBean;
import com.photoeditor.demo.model.image.gl.IPosition;
import com.photoeditor.demo.util.FetcherHolder;
import com.photoeditor.demo.util.IGetCacheListener;
import com.photoeditor.demo.util.ILoadDiskCache;
import com.photoeditor.demo.util.ImageCache;


/**
 * 动态图片预览
 */
public class PlayImageView extends RelativeLayout implements ILoadDiskCache, IPosition {
    private FrameVideoView mVideoView;
    private int mPosition;
    private String mPath;
    public IGetCacheListener mListener;

    public PlayImageView(Context context) {
        super(context);
        initView();
    }

    public PlayImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PlayImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_play_image, this, true);
        mVideoView = (FrameVideoView) findViewById(R.id.video_image);
    }

    public void setVideoImageViewListener(final OnClickListener listener) {
        if (mVideoView != null) {
            mVideoView.setFrameVideoViewListener(new FrameVideoViewListener() {
                @Override
                public void mediaPlayerPrepared(MediaPlayer mediaPlayer) {

                }

                @Override
                public void onViewTap(View view, float x, float y) {
                    listener.onClick(view);
                }
            });
        }
    }

    public void setup(ViewGroup container, BitmapBean bean, boolean isCurrent) {
        mVideoView.setup(bean.mUri, true);
//		mVideoView.setImpl(ImplType.VIDEO_VIEW);
        if (isCurrent) {
            container.addView(this);
        } else {
            container.addView(this, 0);
        }
    }

    public void start() {
        mVideoView.start();
    }

    public void pause() {
        mVideoView.pause();
    }

    public void stop() {
        mVideoView.stop();
    }

    @Override
    public int getPosition() {
        return mPosition;
    }

    @Override
    public void setPosition(int position) {
        mPosition = position;
    }

    @Override
    public void loadDiskCache(ImageCache cache, String path, IGetCacheListener listener) {
        mPath = path;
        mListener = listener;
        FetcherHolder.getSmallImageFetcher().loadPreViewDiskCache(path, this);
    }

    @Override
    public void setCacheMiniBitmap(Bitmap bitmap) {
        ImageView v = getDisplayView();
        if (bitmap != null && !bitmap.isRecycled() && v.getVisibility() == View.VISIBLE) {
            v.setScaleType(ScaleType.FIT_CENTER);
            v.setImageBitmap(bitmap);
        } else if ((bitmap == null || bitmap.isRecycled()) && v.getVisibility() == View.VISIBLE) {
            if (mListener != null) {
                mListener.onLoadError();
            }
        }
    }

    @Override
    public String getPath() {
        return mPath;
    }

    @Override
    public ImageView getDisplayView() {
        return mVideoView.getPlaceholderImageView();
    }
}
